package fr.esiag.isidis.epp.Interface;

import java.text.SimpleDateFormat;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import fr.esiag.isidis.epp.csv.GenerateCsv;
import fr.esiag.isidis.epp.generatedClasses.CdcExportType;
import fr.esiag.isidis.epp.generatedClasses.PointConso;
import fr.esiag.isidis.epp.generatedClasses.Query;
import fr.esiag.isidis.epp.generatedClasses.QueryExportCdc;
import fr.esiag.isidis.epp.search.PointCdcService;
import fr.esiag.isidis.epp.service.JaxbConverter;
import fr.esiag.isidis.epp.util.Utils;

public class TraiteData {

	public TraiteData() {
		// TODO Auto-generated constructor stub
	}
	
	 public void genDataCsv(String beginDate, String endDate, String[] sites, String filename) {
		QueryExportCdc qe = new QueryExportCdc();
		for (String s : sites) {
		Query q = new Query();
		q.setDateDebut(Utils.convertDateToIntSite(beginDate));
		q.setDateFin(Utils.convertDateToIntSite(endDate));
		q.setHeureDebut(0);
		q.setHeureFin(1200);
		q.setIDSite(Utils.transformeIdnn(Utils.getIdnn(Integer.parseInt(s))));
		q.setType(CdcExportType.INDIVIDUEL);
		qe.getQuery().add(q);
		}
		ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/epp-context.xml");
		PointCdcService pt = context.getBean(PointCdcService.class);
		PointConso pc =  pt.traiteQuery(qe);
		GenerateCsv csv = new GenerateCsv(filename,1);
		csv.doCdcCsv(pc);
		csv.close();
	}
	 
	 public void genDataXml(String beginDate, String endDate, String[] sites, String filename) {
		 QueryExportCdc qe = new QueryExportCdc();
			for (String s : sites) {
			Query q = new Query();
			q.setDateDebut(Utils.convertDateToIntSite(beginDate));
			q.setDateFin(Utils.convertDateToIntSite(endDate));
			q.setHeureDebut(0);
			q.setHeureFin(1200);
			q.setIDSite(Utils.transformeIdnn(Utils.getIdnn(Integer.parseInt(s))));
			q.setType(CdcExportType.INDIVIDUEL);
			qe.getQuery().add(q);
			}
			ApplicationContext context = new FileSystemXmlApplicationContext("classpath:/epp-context.xml");
			PointCdcService pt = context.getBean(PointCdcService.class);
			PointConso pc =  pt.traiteQuery(qe);
			JaxbConverter converter = context.getBean(JaxbConverter.class);
			converter.marshallerPointConso(filename+".xml", pc);
		}
	 
	 public static void main(String[] args) throws Exception {
		 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY");
		 String beginDate = ("01/01/2011");
		 String endDate = ("02/01/2011");
		 String [] sites = {"1","2"};
		 String filename = "toto";
		TraiteData t = new TraiteData();
		t.genDataCsv(beginDate, endDate, sites, filename);
		t.genDataXml(beginDate, endDate, sites, filename);
	}
}
